package com.baidu.springcloud.controller;

import com.baidu.springcloud.service.OrderHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ShiLei
 * @version v1.0
 * @date 2020-03-18 16:25
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "globol")
public class OrderHystrixController {

    @Resource
    private OrderHystrixService orderHystrixService;

    @GetMapping("consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return orderHystrixService.paymentInfo_OK(id);
    }

    //    @HystrixCommand(fallbackMethod = "handler", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
//    })
    @HystrixCommand
    @GetMapping("consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        return orderHystrixService.paymentInfo_TimeOut(id);
    }

    public String handler(@PathVariable("id") Integer id) {
        return "线程池：  " + Thread.currentThread().getName() + " 80访问超时，请重新尝试 ,id: " + id + "/(ㄒoㄒ)/~~";

    }

    public String globol() {
        return "服务器内部错误~";
    }
}
